package com.william.taskrobot.service;

import com.alibaba.fastjson.JSON;
import com.william.taskrobot.model.RuleInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.nio.charset.Charset;

/**
 * RuleFactory class
 * 创建rule，保存rule
 * @author william
 * @date 2021/1/14
 */
public class RuleFactory {
    private static String fileName="rule.json";
    public static RuleInfo create() throws Exception{
        String path= ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + fileName)
                .getAbsoluteFile().getPath();
        String content= FileUtils.readFileToString(new File(path), Charset.forName("utf-8"));
        RuleInfo ruleInfo= JSON.parseObject(content,RuleInfo.class);
        return ruleInfo;
    }
    public static void save(RuleInfo info) throws Exception{
        File file = ResourceUtils.getFile("classpath:"+fileName);
        String content=JSON.toJSONString(info);
        FileUtils.write(file,content,Charset.forName("utf-8"));
    }
}
